import axios from 'axios'
axios.defaults.baseURL = '';
axios.defaults.headers['Content-Type'] = 'application/json;charset=UTF-8';
axios.defaults.timeout = 1000;

axios.defaults.baseURL = '';
axios.defaults.headers['Content-Type'] = 'application/json;charset=UTF-8';
axios.defaults.timeout = 1000;
axios.interceptors.request.use(req => {
    let token = sessionStorage.getItem('token');
    token && (req.headers['token'] = token);
    return req;
}, err => {
    return Promise.reject(err);
})

axios.interceptors.response.use(res => {
    res.data.token && sessionStorage.setItem('token', res.data.token);
    return res;
}, (err) => {
    switch (err.response.status) {
        case 401:
            sessionStorage.removeItem('token');
            window.location.href = '/home';
    }
    return Promise.reject(err);
})
export default axios;